<?	
	$conn = mysql_connect("localhost", "kyr", "a13579");
	$db = mysql_select_db("my_database", $conn);

	$response_msg = array(	200 => 'OK',
							210 => 'Go',
							221 => 'Service Closing',
							230 => 'User Logged in',
							241 => 'Success to buy',
							251 => 'Permit',
							531 => 'Invalid id or password',
							551 => 'Not Permit'
						);
		
	$request_msg = $_POST['request_msg'];
	$split_msg = explode(' ', $request_msg);

	function sendmsg($code, $word1=Null, $word2=Null){
		GLOBAL $response_msg;

		if($code == 210)
			$msg = $response_msg[$code] . " " . $word1;
		else
			$msg = $response_msg[$code];
		echo $msg;
	}

	function check($user_id, $what){
		GLOBAL $conn;

		if($what == 'version'){
			$ver_query = "SELECT pro FROM version WHERE id = '$user_id'";
			$ver_result = mysql_query($ver_query, $conn);
			$ver_row = mysql_fetch_array($ver_result);

			if($row[pro] == 'no')
				return 'standard';
			else
				return 'pro';
		}else if($what == 'money'){
			$money_query = "SELECT money FROM study WHERE id = '$user_id'";
			$money_result = mysql_query($money_query, $conn);
			$money_row = mysql_fetch_array($money_result);

			return $row[money];
		}
	}
	
	if($split_msg[0] == "USER"){
		$log_query = "SELECT count(*) FROM login WHERE id = '$split_msg[1]' and pw = '$split_msg[2]'";
		$log_result = mysql_query($log_query, $conn);
		$log_row = mysql_fetch_array($log_result);

		if($log_row[0] == 1){
			sendmsg(230);
		}else{
			sendmsg(531);
		}
	}else if($split_msg[0] == "STAGE"){
			sendmsg(210, $split_msg[1]); // 200 OK를 보내야할 부분
	}else if($split_msg[0] == "CHECK"){
		if(check($split_msg[1], 'version') == 'standard'){
			sendmsg(551);
		}else{
			sendmsg(251);
		}
	}else if($split_msg[0] == "QUIT"){
		sendmsg(221);	
	}else if($split_msg[0] == "CURRENT"){
		sendmsg(210, $split_msg[1], $split_msg[3]);
	}else{
		sendmsg(531);
	}
?>